<?php

class Admin extends DataExtension {
	private static $db = array('IsAdmin' => 'Boolean');
	
	function updateFieldLabels(&$labels) {
		$labels['IsAdmin'] = _t('Admin.IS_ADMIN', 'Is Admin?');
		$labels['IsAdmin.Nice'] = _t('Admin.IS_ADMIN', 'Is Admin?');
    }
	
	function updateCMSFields(FieldList $fields) {
		$fields->removeByName('IsAdmin');
	}
	
	function updateFrontendFields(FieldList $fields) {
        $fields->removeByName('IsAdmin');
    }
	
	function onBeforeWrite(){
		if(!$this->owner->IsAdmin && $this->owner->Email == Security::default_admin_username()) {
            $this->owner->IsAdmin = 1;
        }
		
		if($this->owner->Groups()->find('IsAdminGroup', 1)) {
            $this->owner->IsAdmin = 1;
        }
	}
}
